﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

using Api.Hub.Models;

namespace Api.Client.Proxy
{
    public class PersonProxy : BaseProxy
    {
        public List<Person> Get(int? id = null)
        {
            var url = base.ApiHost + "/api/person/" + id;
            HttpClient client = new HttpClient();
            var json = client.GetStringAsync(url).Result;
            var persons = JsonConvert.DeserializeObject<List<Person>>(json);
            return persons;
        }

        public Person Post(Person person)
        {
            var url = base.ApiHost + "/api/person";
            HttpClient client = new HttpClient();
            var json = JsonConvert.SerializeObject(person);
            var bytes = Encoding.UTF8.GetBytes(json);
            var content = new ByteArrayContent(bytes);
            content.Headers.Add("Content-Type", "application/json");
            content.Headers.Add("Content-Length", bytes.Length.ToString());
            //var content = new StringContent(json, Encoding.UTF8, "application/json");
            var message = client.PostAsync(url, content).Result;
            var _json = message.Content.ReadAsStringAsync().Result;
            var _person = JsonConvert.DeserializeObject<Person>(_json);
            return _person;
        }
    }
}
